package com.sdkx.app.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

public class BrokenLineView extends android.support.v7.widget.AppCompatImageView {
    float[] xs = new float[8];
    Paint mPaint;
    int lineWidth = 1;
    int lineColor = Color.parseColor("#FF0000");

    public int getLineWidth() {
        return lineWidth;
    }

    public int getLineColor() {
        return lineColor;
    }

    public BrokenLineView(Context context) {
        super(context);
    }

    public BrokenLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BrokenLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        xs[0] = 0;
        xs[1] = getMeasuredHeight() / 2;
        xs[xs.length - 2] = getMeasuredWidth();
        xs[xs.length - 1] = getMeasuredHeight() / 2;
        int divider = getMeasuredWidth() / 3;
        for (int index = 2; index < xs.length - 2; index += 2) {
            xs[index] = xs[index - 2] + divider;
            xs[index + 1] = ((index >> 1) % 2) * (getMeasuredHeight() >> 1) + (getMeasuredHeight() >> 2);
        }
    }

    public void setLineWidth(int width) {
        lineWidth = width > 0 ? width : 1;
        if (mPaint != null) {
            mPaint.setStrokeWidth(lineWidth);

        }
    }

    public void setLineColor(int color) {
        lineColor = color;
        if (mPaint != null) {
            mPaint.setColor(lineColor);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(lineWidth);
            mPaint.setColor(lineColor);
        }
        Path _path = new Path();
        _path.moveTo(xs[0], xs[1]);
        for (int i = 2; i < xs.length; i += 2) {
            _path.lineTo(xs[i], xs[i + 1]);
        }
        canvas.drawPath(_path, mPaint);
    }

    public static Bitmap getBitmap(int width, int height, int linew, int linec) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        int[] data = new int[8];
        data[0] = 0;
        data[1] = height/2;
        data[data.length-2] = width;
        data[data.length-1] = height/2;
        int divider = width / 3;
        for (int index = 2; index < data.length - 2; index += 2) {
            data[index] = data[index - 2] + divider;
            data[index + 1] = ((index >> 1) % 2) * (height >> 1) + (height >> 2);
        }
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(linew);
        paint.setColor(linec);
        Path _path = new Path();
        _path.moveTo(data[0], data[1]);
        for (int i = 2; i < data.length; i += 2) {
            _path.lineTo(data[i], data[i + 1]);
        }
        canvas.drawPath(_path, paint);
        //   canvas.drawLine(0,0,width,height,paint);
        return bitmap;
    }
}
